﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Sunny.UI;
using System.IO;
using BaseDll;
namespace UserData
{
    public partial class Form_数据统计 : UIPage
    {
        public class TestTime_数据统计
        {
            public int n_Total = 0;
            public int n_OK = 0;
            public double d_CTAllTime = 0;
            public double d_CTAllCount = 0;
        }
        string str_PathName = AppDomain.CurrentDomain.BaseDirectory + @"\Config.设备运行数据统计.json";
        TestTime_数据统计 testTime = new TestTime_数据统计();
        public Form_数据统计()
        {
            InitializeComponent();

        }

        private void ReflushData()
        {
            if (InvokeRequired)
            {
                BeginInvoke(new Action(() => ReflushData()));
            }
            else
            {
                lb_OKCount.Text = testTime.n_OK.ToString();
                lb_NGCount.Text = (testTime.n_Total - testTime.n_OK).ToString();
                lb_TotalCount.Text = testTime.n_Total.ToString();
                lb_OKRate.Text = (((double)testTime.n_OK / (double)testTime.n_Total)*100).ToString("0.00")+ "%";
                lb_UPH.Text = (3600.0 * testTime.d_CTAllCount / testTime.d_CTAllTime).ToString("0.00");
            }
        }

        public void AddTestTime(double n_用时时间,bool b_Result)
        {
            if(b_Result)
            {
                testTime.n_Total++;
                testTime.n_OK++;
                if(n_用时时间>0)
                {
                    testTime.d_CTAllCount++;
                    testTime.d_CTAllTime = testTime.d_CTAllTime + n_用时时间;
                }
            }
            else
            {
                testTime.n_Total++;
            }
            ReflushData();
            AccessJosnSerializer.ObjectToJson(str_PathName, testTime);
        }

        public TestTime_数据统计 Function_获取统计数据()
        {
            return testTime;
        }
        private void Form_数据统计_Load(object sender, EventArgs e)
        {
            if (!File.Exists(str_PathName))
            {
                AccessJosnSerializer.ObjectToJson(str_PathName, testTime);
            }
            else
            {
                object obj = AccessJosnSerializer.JsonToObject(str_PathName, typeof(TestTime_数据统计));
                if (obj != null)
                {
                    testTime = (TestTime_数据统计)obj;
                }
            }
            ReflushData();
        }

        private void tableLayoutPanel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void 清除数据ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            testTime = new TestTime_数据统计();
            AccessJosnSerializer.ObjectToJson(str_PathName, testTime);
            ReflushData();
        }
    }
}
